import Vue from 'vue';
import VueRouter from 'vue-router';

import login from '../components/login.vue';
import home from '../components/home.vue';
import welCome from '../components/welCome.vue';
import users from '../components/user/users.vue';
import rights from '@/components/power/rights.vue';
import Roles from '@/components/power/Roles.vue';
import Cate from '@/components/goods/Cate.vue';
import Params from '@/components/goods/Params.vue';
import GoodsList from '@/components/goods/List.vue';
import Add from '@/components/goods/Add.vue';
import order from '@/components/order/order.vue';
import Report from '@/components/report/Report.vue';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    component: login,
  },
  {
    path: '/home',
    component: home,
    redirect: '/welCome',
    children: [
      {
        path: '/welCome',
        component: welCome,
      },
      {
        path: '/users',
        component: users,
      },
      {
        path: '/rights',
        component: rights,
      },
      {
        path: '/Roles',
        component: Roles,
      },
      {
        path: '/categories',
        component: Cate,
      },
      {
        path: '/params',
        component: Params,
      },
      {
        path: '/goods',
        component: GoodsList,
      },
      {
        path: '/goods/add',
        component: Add,
      },
      {
        path: '/orders',
        component: order,
      },
      {
        path: '/reports',
        component: Report,
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 挂载全局路由守卫
router.beforeEach((to, from, next) => {
  // 获取本地token
  const login_token = window.sessionStorage.getItem('token');
  if (to.path === '/login') {
    return next();
  } else if (!login_token) {
    return next('/login');
  } else {
    next();
  }
});

export default router;
